<script type="text/javascript" language="javascript">
function showPreloader()
{
	var returnVal = validateMapReport()
	if(returnVal == 1)
	{
		document.getElementById('popup_div').innerHTML = '<div id="loading_txt" >Loading...</div>';
		document.frm_map_filter.submit();
	}
}
function changeValue()
{
	var tmpId = '';
	var sel = 0;
	
	for (i=0; i<document.getElementById('selContId').length;i++)
	{
		if (document.getElementById('selContId').options[i].selected)
		{
			if(sel < 4)
			{
				if(i==0)
					tmpId = document.getElementById('selContId').options[i].value+",";
				else
					tmpId +=document.getElementById('selContId').options[i].value+",";
				
				sel++;
			}
			else
			{
				document.getElementById('selContId').options[i].selected = false;
				alert("Any 4 contacts only allowed to choose");
				sel--;
			}
		}
	}
	document.getElementById('txtContIds').value = tmpId;
	
}

jQuery(function() 
{

 $("#time3, #time4").timePicker({
  startTime: "00:01 AM", // Using string. Can take string or Date object.
  endTime: "11:59 PM", // Using Date object here.
  show24Hours: false,
  separator: ':',
  step: 1});    
	
var oldTime = $.timePicker("#time3").getTime();


if($("#rdAlertBy option:selected").text() == "OverStay" || $("#rdAlertBy option:selected").text() == "Over Stay")
	$("#trDoNot").show();
else
	$("#trDoNot").hide();

$("#time3").change(function() {
  if ($("#time4").val()) { // Only update when second input has a value.
	// Calculate duration.
	var duration = ($.timePicker("#time4").getTime() - oldTime);
	var time = $.timePicker("#time3").getTime();
	// Calculate and update the time in the second input.
	$.timePicker("#time4").setTime(new Date(new Date(time.getTime() + duration)));
	oldTime = time;
  }
});
// Validate.
$("#time4").change(function() {
  if($.timePicker("#time3").getTime() > $.timePicker(this).getTime()) {
	$(this).addClass("error");
  }
  else {
	$(this).removeClass("error");
  }
});

});
function hidePreLoader()
{
	document.getElementById('popup_div').innerHTML = '&nbsp;';
}
function funEditUser(did,cid,act)
{
	//alert(uid)
	document.frmSubmit.txtAlertId.value = did;
	document.frmSubmit.txtClientId.value = cid;
	document.frmSubmit.action = act;
	document.frmSubmit.submit();
}
function days_between(date1, date2) {

    var ONE_DAY = 1000 * 60 * 60 * 24

    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

	var difference_ms = date1_ms - date2_ms
	    
    return Math.round(difference_ms/ONE_DAY)

}


function sendCSVData()
{
	document.frmTripData.submit();
}
$(function() {
	$("#nav a").click(function()
	{
		//$("#current").removeAttr("id");
		//$(this).attr("id", "current");
		$("#content").load($(this).attr("id") + '.php');
		// Prevent click from jumping to the top of the page
		return false;
	});
});
var ajax1=new sack();
function showAlertBy(val)
{
	if(document.frmAddAlert.map_device_id.value !="")
	{
		qry = "select count(tdai_id) from tb_device_alert_info where tdai_clientId = <?php echo $_SESSION[clientID];?> and tdai_status = 0 and tdai_deviceId = "+document.frmAddAlert.map_device_id.value+" and tdai_alertBy = '"+val+"'";
		ajax1.requestFile = 'ajax_server.php?getResQry='+qry;
		//alert(ajax1.requestFile);
		ajax1.onCompletion = function()
		{
			//alert(ajax1.response)
			if(ajax1.response >= 5)
			{
				alert("This  alert type reached Max. Limit of 5. Choose different Alert Method");
				document.frmAddAlert.rdAlertBy.selectedIndex = 0;
			}
			else
			{
				 showHideMethod(val);
			}
		};
		ajax1.runAJAX();
	}
	else
	{
		 showHideMethod(val);
	}
	
}

function showHideMethod(val)
{
		if(val == "Date")
		{
			document.getElementById('trAlertLabel').innerHTML='Choose Date';
			document.getElementById('spanAlertDate').style.display='block';
			document.getElementById('spanOdoMRead').style.display='none';
			document.getElementById('spanOverSpeed').style.display='none';
			document.getElementById('spanOverStay').style.display='none';
			document.getElementById('spanLowBattery').style.display='none';
			document.getElementById('txtAlertDate').value = "";
			$("#trDoNot").hide();
		}
		else if(val == "Meter")
		{
			document.getElementById('trAlertLabel').innerHTML='Enter Odometer';
			document.getElementById('spanAlertDate').style.display='none';
			document.getElementById('spanOdoMRead').style.display='block';
			document.getElementById('spanOverSpeed').style.display='none';
			document.getElementById('spanOverStay').style.display='none';
			document.getElementById('spanLowBattery').style.display='none';
			document.getElementById('txtOdoMRead').value = "";
			$("#trDoNot").hide();
		}
		else if(val == "OverSpeed" || val  == "Immbolize")
		{
			document.getElementById('trAlertLabel').innerHTML='Speed Limit';
			document.getElementById('spanAlertDate').style.display='none';
			document.getElementById('spanOdoMRead').style.display='none';
			document.getElementById('spanOverSpeed').style.display='block';
			document.getElementById('spanOverStay').style.display='none';
			document.getElementById('spanLowBattery').style.display='none';
			$("#trDoNot").hide();
			//document.getElementById('spanOverSpeed').value = "";
		}
		else if(val == "OverStay")
		{
			document.getElementById('trAlertLabel').innerHTML='Duration Limit';
			document.getElementById('spanAlertDate').style.display='none';
			document.getElementById('spanOdoMRead').style.display='none';
			document.getElementById('spanOverSpeed').style.display='none';
			document.getElementById('spanOverStay').style.display='block';
			document.getElementById('spanLowBattery').style.display='none';
			$("#trDoNot").show();
			document.getElementById('txtOverStay').value = "";
		}
		else if(val == "LowBattery")
		{
			document.getElementById('trAlertLabel').innerHTML='Low Battery Limit';
			document.getElementById('spanAlertDate').style.display='none';
			document.getElementById('spanOdoMRead').style.display='none';
			document.getElementById('spanOverSpeed').style.display='none';
			document.getElementById('spanOverStay').style.display='none';
			document.getElementById('spanLowBattery').style.display='block';
			$("#trDoNot").hide();
			//document.getElementById('txtLowBattery').value = "";
		}
		else if(val == "SOS" || val == "Ignition" || val == "FuelTheft")
		{
			document.getElementById('trAlertLabel').innerHTML='&nbsp;';
			document.getElementById('spanAlertDate').style.display='none';
			document.getElementById('spanOdoMRead').style.display='none';
			document.getElementById('spanOverSpeed').style.display='none';
			document.getElementById('spanOverStay').style.display='none';
			document.getElementById('spanLowBattery').style.display='none';
			$("#trDoNot").hide();
		}
		else
		{
			//alert(val)
			if(val.indexOf("_") > 0)
			{
			document.getElementById('trAlertLabel').innerHTML='&nbsp;';
			document.getElementById('spanAlertDate').style.display='none';
			document.getElementById('spanOdoMRead').style.display='none';
			document.getElementById('spanOverSpeed').style.display='none';
			document.getElementById('spanOverStay').style.display='none';
			document.getElementById('spanLowBattery').style.display='none';
			$("#trDoNot").hide();
			}
		}
}

var minVal;

var m1 = '<?php if($speedLimit[0]) echo $speedLimit[0]; else echo 0;?>';
for(k=10; k <= 300; k=k+10)
{
	if(m1!=0 && k==m1)
	{
		minVal +='<option value='+k+' selected="selected">'+k+'</option>';
	}
	else if(m1 == 0)
	{
		if(k == 20)
		{
			minVal +='<option value='+k+' selected="selected">'+k+'</option>';
		}
		else
		{
			minVal +='<option value='+k+'>'+k+'</option>';
		}
	}
}

var maxVal;
var m2 = '<?php if($speedLimit[1]) echo $speedLimit[1]; else echo 0;?>';
for(j=10; j <= 300; j=j+10)
{
	if(m2!=0)
	{
		if(j == m2)
		{
			maxVal +='<option value='+j+' selected="selected">>= '+j+' kmph</option>';
		}
		else
		{
			maxVal +='<option value='+j+'>>= '+j+' kmph</option>';
		}
	}
	else if(m2 == 0)
	{
		if(j==80)
		{
			maxVal +='<option value='+j+' selected="selected">>= '+j+' kmph</option>';
		}
		else
		{
			maxVal +='<option value='+j+'>>= '+j+' kmph</option>';
		}
	}
}

function changeSts(uid)
{
	t = confirm("Are your sure to delete this Alert?");
	if(t)
	{
		qry = "Delete from tb_device_alert_info WHERE tdai_id = "+uid;
		ajax1.requestFile = 'ajax_server.php?ajaxQry='+qry;
		//alert(ajax1.requestFile);
		ajax1.onCompletion = function(){resultUserStatus()};
		ajax1.runAJAX();
	}
}
function resultUserStatus()
{
	//alert(ajax1.response)
  if(ajax1.response)
  {
	 alert("Status Updated Successfully.");
	 window.location.href = "?ch=myalerts";
  }
  else
  {
	  alert("Status Not Updated.");
  }
	
}
function submitAlertType(val)
{
	ajax1.requestFile = 'ajax_server.php?contType='+val;			
	//alert(ajax1.requestFile);
	ajax1.onCompletion = function(){getSource()};
	document.getElementById('selContId').innerHTML = '</option value=0>Loading...</option>';
	ajax1.runAJAX();
}
function getSource()
{
	document.getElementById('txtContIds').value="";
	document.getElementById('selContId').innerHTML = ajax1.response;
}
 jQuery(document).ready( function() {
	// binds form submission and fields to the validation engine
	jQuery("#frmAddAlert").validationEngine();
	$("#txtAlertDate" ).datepicker({
		changeMonth: true,
		changeYear: true,
		showOn: "button",
		minDate: 1,
		buttonImage: "images/calendar.gif",
		buttonImageOnly: true,
		dateFormat : "dd-mm-yy"
	});
});
function getDeviceDetails(devId)
{
	document.frmAddAlert.rdAlertBy.selectedIndex = 0;	
	ajax1.requestFile = 'ajax_server.php?getAlertListfor='+devId;			
	//alert(ajax1.requestFile);
	ajax1.onCompletion = function(){getAlertList()};
	//document.getElementById('selContId').innerHTML = '</option value=0>Loading...</option>';
	ajax1.runAJAX();
	
}
function getEditDeviceDetails(devId,type)
{
	ajax1.requestFile = 'ajax_server.php?getAlertListfor='+devId;			
	//alert(ajax1.requestFile);
	ajax1.onCompletion = function(){getAlertList(type)};
	//document.getElementById('selContId').innerHTML = '</option value=0>Loading...</option>';
	ajax1.runAJAX();
	
}
function removeOptionLast(lim)
{
  var elSel = document.getElementById('rdAlertBy');
  for(t = lim;t < elSel.length; t++)
  {
    elSel.remove(t);
  }
}
function getAlertList(type)
{
	var alrtListArr = ajax1.response.split("#");
	var obj = document.getElementById('rdAlertBy');
	var selLengh = 6;
	removeOptionLast(selLengh)
	for(k=0;k<alrtListArr.length-1;k++)
	{
		var portVal = alrtListArr[k].split("_");
		/*if(portVal[1] != 0)
		{*/
			if(type == alrtListArr[k])
			{
				obj.options[selLengh] = new Option(portVal[0], alrtListArr[k]);
				obj.options[selLengh].selected = true;
			}
			else
			{
				obj.options[selLengh] = new Option(portVal[0], alrtListArr[k]);
			}
			selLengh++;
		//}
	}
}
function changeAlerts(alts)
{
	document.frmTypeSubmit.txtAlertType.value = alts;
	document.frmTypeSubmit.submit();
}
</script>
<?php
//print_r($_POST);
if(isset($_POST[txtAlertId]) && $_POST[txtAlertId] !='')
{
	$sql = "SELECT * FROM tb_device_alert_info WHERE tdai_id =".$_POST[txtAlertId];
	$rows = $db->query($sql);
	if($db->affected_rows > 0)
	{
		$deviceRecord = $db->fetch_array($rows);
		$spltTime = explode("#",$deviceRecord[tdai_donotTime]);	
		if($deviceRecord[tdai_alertBy] == "OverSpeed" || $deviceRecord[tdai_alertBy] == "Immbolize")
		{
			$speedLimit = explode("#",$deviceRecord[tdai_alertSrc]);	
			
			//print_r($spltTime);
		}
		else if(strpos($deviceRecord[tdai_alertBy],"_") >= 0)
		{
			?>
            <script language="javascript">
			getEditDeviceDetails('<?php echo $deviceRecord[tdai_deviceId ];?>','<?php echo $deviceRecord[tdai_alertBy];?>');
			</script>
            <?php
		}
		if($deviceRecord['tdai_alertType'] == "" && $deviceRecord['tdai_source'] == "")
		{
			$contId = explode(",",$deviceRecord['tdai_contactIds']);
		}
	}
}

function dateDiff($endDate, $beginDate)
{
	 //explode the date by "-" and storing to array
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   //gregoriantojd() Converts a Gregorian date to Julian Day Count
   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   return $end_date - $start_date;

}
if($recordUserInfo[ci_clientType] == "Client" && $recordUserInfo[ui_isAdmin] == "1")
{
	$devices_query =  "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_clientId =".$_SESSION[clientID]." AND di_status = 1 ORDER BY di_deviceName,di_deviceId ASC";
}
else if($recordUserInfo[ci_clientType] == "Client" && $recordUserInfo[ui_isAdmin] == "0" && $recordUserInfo[ui_roleId] == "1")
{
	$devices_query = "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_status = 1 AND di_clientId=".$_SESSION[clientID]." AND di_assignedUserId = ".$_SESSION[userID]." ORDER BY di_deviceName,di_deviceId ASC";
}
$devices_resp = mysql_query($devices_query);	
?>


<table border="0" width="95%">
<!--<tr>
	<td>&nbsp;</td>
	<td align="left">Show Alerts&nbsp;
    	<select id="selAlert" name="selAlert" onchange="changeAlerts(this.value)">
        	<option <?php if($_POST[txtAlertType] == "up") echo 'selected="selected""';?> value="up">Upcoming</option>
            <option <?php if($_POST[txtAlertType] == "gone") echo 'selected="selected""';?> value="gone">Delivered</option>
            <option <?php if($_POST[txtAlertType] == "all") echo 'selected="selected""';?> value="all">All</option>            
        </select>
    
    </td>
</tr>-->
<tr>
<td width="35%" style="vertical-align:top;">
<form id="frmAddAlert" name="frmAddAlert" method="post" action=""> 
<input type="hidden" name="txtAlertId" id="txtAlertId" value="<?php echo $_POST[txtAlertId];?>" />
<input type="hidden" name="txtClientId" id="txtClientId" value="<?php echo $_POST[txtClientId];?>" />
<table class="gridform_final">
<tr><th colspan="2">Add Alert</th></tr>
  <tr>
    <td width="30%" align="right">Select Device&nbsp;</td>
    <td align="left">
        <select name="map_device_id" id="map_device_id" tabindex="1" style="width:80%" class="validate[required]" onChange="getDeviceDetails(this.value)">
        <option value="">Select Device</option>
         <?php 
		while($devices_fetch = @mysql_fetch_assoc($devices_resp)) 
		{ 
			$renewDate = date("d-m-Y",strtotime("-1 days ".($devices_fetch[tcs_noOfMonths]) ."months ".$devices_fetch[tcs_renewalDateFrom]));
			//echo $date_offline." <= ".date("d-m-Y",strtotime($renewDate))."<br>";
			if(strtotime(date('d-m-Y')) <= strtotime($renewDate))
			{
			if($devices_fetch[di_deviceName])
				$devName = $devices_fetch[di_deviceName];
			else
				$devName = $devices_fetch[di_deviceId];
        ?>
        <option value="<?php echo $devices_fetch[di_id]; ?>" 
        <?php if($deviceRecord[tdai_deviceId] == $devices_fetch[di_id]) echo "selected"; ?>><?php echo $devName; ?></option>
        <?php } }?>		
        </select>
        <input type="hidden" name="curdate" id="curdate" value="<?php echo date('Y-m-d'); ?>" />
    </td>
    </tr>
  <tr>
    <td align="right">Purpose</td>
    <td align="left">
            <input type="text" name="txtPurpose" id="txtPurpose" class="validate[required] text-input" maxlength="20" tabindex="2" style="width:80%" value="<?php echo $deviceRecord[tdai_purpose];?>" />
    </td>
  </tr>              
  <tr>
    <td align="right">Alert Type </td>
    <td align="left">
    
    	<select name="rdAlertBy" id="rdAlertBy" tabindex="3" style="width:80%" class="validate[required]" onChange="showAlertBy(this.value);">
        	<option value="">Select Alert By</option>
        	<option value="Date" <?php if($deviceRecord[tdai_alertBy] == 'Date') echo 'selected="selected"';?>>Date</option>
            <option value="Meter" <?php if($deviceRecord[tdai_alertBy] == 'Meter') echo 'selected="selected"';?>>Odo Meter</option>
            <option value="OverSpeed" <?php if($deviceRecord[tdai_alertBy] == 'OverSpeed') echo 'selected="selected"';?>>Over Speed</option>
            <option value="OverStay" <?php if($deviceRecord[tdai_alertBy] == 'OverStay') echo 'selected="selected"';?>>Over Stay</option>
            <option value="LowBattery" <?php if($deviceRecord[tdai_alertBy] == 'LowBattery') echo 'selected="selected"';?>>Low Battery</option>
           <!-- <option value="SOS" <?php if($deviceRecord[tdai_alertBy] == 'SOS') echo 'selected="selected"';?>>SOS</option>
            <option value="Ignition" <?php if($deviceRecord[tdai_alertBy] == 'Ignition') echo 'selected="selected"';?>>Ignition</option>
            <option value="FuelTheft" <?php if($deviceRecord[tdai_alertBy] == 'FuelTheft') echo 'selected="selected"';?>>Fuel Theft</option>
            <option value="Immbolize" <?php if($deviceRecord[tdai_alertBy] == 'Immbolize') echo 'selected="selected"';?>>Over Speed Alarm</option>-->
        </select>
        <!--<input type="radio" class="validate[required] radio" name="rdAlertBy" <?php if(isset($deviceRecord[tdai_alertBy])) { if($deviceRecord[tdai_alertBy] == 'Date') echo 'checked="checked"'; } else echo 'checked="checked"';?> id="rdDateAlert" value="Date" onclick="showAlertBy(this.value);" />Date
        <input type="radio" class="validate[required] radio" <?php if(isset($deviceRecord[tdai_alertBy])) { if($deviceRecord[tdai_alertBy] == 'Meter') echo 'checked="checked"'; } ?> name="rdAlertBy" id="rdKmAlert" value="Meter" onclick="showAlertBy(this.value);" />Odo Meter-->
    </td>
    </tr>   
  <tr>
        <td align="right" id="trAlertLabel"><?php 
			if(isset($deviceRecord[tdai_alertBy])) { 
				if($deviceRecord[tdai_alertBy] == 'Date') 
					echo 'Choose Date'; 
				elseif($deviceRecord[tdai_alertBy] == 'Meter') 
					echo 'Enter Odometer';
				elseif($deviceRecord[tdai_alertBy] == 'OverSpeed' || $deviceRecord[tdai_alertBy] == 'Immbolize') 
					echo 'Speed Limit';
				elseif($deviceRecord[tdai_alertBy] == 'OverStay') 
					echo 'Duration Limit';
				elseif($deviceRecord[tdai_alertBy] == 'LowBattery') 
					echo 'Low Battery Limit';
				elseif($deviceRecord[tdai_alertBy] == 'SOS' || $deviceRecord[tdai_alertBy] == 'Ignition' || $deviceRecord[tdai_alertBy] == 'FuelTheft') 
					echo '&nbsp;'; } 
				else echo 'Choose Date';?></td>
        <td align="left">
        <?php
		if(isset($deviceRecord[tdai_alertBy])) 
		{ 
			if($deviceRecord[tdai_alertBy] == 'Date') 
			{
				?>
                 <span id="spanAlertDate" style="display:block">
                <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOdoMRead" style="display:none">
                <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOverSpeed" style="display:none">
                Min &nbsp;<select name="sdMinSpeed" id="sdMinSpeed" class="validate[required] text-input">
                				<option value="20">20</option>
                                <option value="40">40</option>
                                <option value="60" selected="selected">60</option>
                                <option value="80">80</option>
                                <option value="100">100</option>
                                <option value="120">120</option>
                                <option value="140">140</option>
                                <option value="160">160</option>
                                <option value="180">180</option>
                                <option value="200">200</option>
                                <option value="220">220</option>
                                <option value="240">240</option>
                                <option value="260">260</option>
                                <option value="280">280</option>
                                <option value="300">300</option>
                            </select>
                 Max &nbsp;<select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
                				<option value="20">20</option>
                                <option value="40">40</option>
                                <option value="60">60</option>
                                <option value="80">80</option>
                                <option value="100">100</option>
                                <option value="120" selected="selected">120</option>
                                <option value="140">140</option>
                                <option value="160">160</option>
                                <option value="180">180</option>
                                <option value="200">200</option>
                                <option value="220">220</option>
                                <option value="240">240</option>
                                <option value="260">260</option>
                                <option value="280">280</option>
                                <option value="300">300</option>
                       </select>
                
                </span>
                <span id="spanOverStay" style="display:none">
                <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>&nbsp; in Mins
                </span>
                <span id="spanLowBattery" style="display:none">
                <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
                </span>
                <?php
			}
			elseif($deviceRecord[tdai_alertBy] == 'Meter') 
			{
				?>
                <span id="spanAlertDate" style="display:none">
                <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOdoMRead" style="display:block">
                <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOverSpeed" style="display:none">
               Min &nbsp;<select name="sdMinSpeed" id="sdMinSpeed" class="validate[required] text-input">
                				<option value="20">20</option>
                                <option value="40">40</option>
                                <option value="60" selected="selected">60</option>
                                <option value="80">80</option>
                                <option value="100">100</option>
                                <option value="120">120</option>
                                <option value="140">140</option>
                                <option value="160">160</option>
                                <option value="180">180</option>
                                <option value="200">200</option>
                                <option value="220">220</option>
                                <option value="240">240</option>
                                <option value="260">260</option>
                                <option value="280">280</option>
                                <option value="300">300</option>
                            </select>
                 Max &nbsp;<select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
                				<option value="20">20</option>
                                <option value="40">40</option>
                                <option value="60">60</option>
                                <option value="80">80</option>
                                <option value="100">100</option>
                                <option value="120" selected="selected">120</option>
                                <option value="140">140</option>
                                <option value="160">160</option>
                                <option value="180">180</option>
                                <option value="200">200</option>
                                <option value="220">220</option>
                                <option value="240">240</option>
                                <option value="260">260</option>
                                <option value="280">280</option>
                                <option value="300">300</option>
                       </select>
                </span>
                <span id="spanOverStay" style="display:none">
                <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>&nbsp; in Mins
                </span>
                <span id="spanLowBattery" style="display:none">
                <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
                </span>
            <?php
			}
			elseif($deviceRecord[tdai_alertBy] == 'OverSpeed' || $deviceRecord[tdai_alertBy] == 'Immbolize') 
			{
				?>
                <span id="spanAlertDate" style="display:none">
                <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOdoMRead" style="display:none">
                <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOverSpeed" style="display:block">
               <input type="hidden" id="sdMinSpeed" name="sdMinSpeed" value="10" />
               <select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
							<script language="javascript">
                                document.write(maxVal);
                            </script>
                   </select>
                </span>
                <span id="spanOverStay" style="display:none">
                <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>&nbsp; in Mins
                </span>
                <span id="spanLowBattery" style="display:none">
                    <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
                    </span>
                <?php
			}
			elseif($deviceRecord[tdai_alertBy] == 'OverStay') 
			{
				?>
                 <span id="spanAlertDate" style="display:none">
                <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOdoMRead" style="display:none">
                <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOverSpeed" style="display:none">
                <input type="hidden" id="sdMinSpeed" name="sdMinSpeed" value="10" />
                <select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
							<script language="javascript">
                                document.write(maxVal);
                            </script>
                   </select>

                </span>
                <span id="spanOverStay" style="display:block">
                <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>&nbsp; in Mins
                </span>
                <span id="spanLowBattery" style="display:none">
                <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
                </span>
                <?php
			}			
			elseif($deviceRecord[tdai_alertBy] == 'LowBattery') 
			{
				?>
                 <span id="spanAlertDate" style="display:none">
                <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOdoMRead" style="display:none">
                <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOverSpeed" style="display:none">
               <input type="hidden" id="sdMinSpeed" name="sdMinSpeed" value="10" />
               <select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
							<script language="javascript">
                                document.write(maxVal);
                            </script>
                   </select>

                </span>
                <span id="spanOverStay" style="display:none">
                <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>&nbsp; in Mins
                </span>
                <span id="spanLowBattery" style="display:block">
                <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
                </span>
                <?php
			}
			elseif($deviceRecord[tdai_alertBy] == 'SOS' || $deviceRecord[tdai_alertBy] == 'Ignition' || $deviceRecord[tdai_alertBy] == 'FuelTheft') 
			{
				?>
                 <span id="spanAlertDate" style="display:none">
                <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
                </span>
                <span id="spanOdoMRead" style="display:none">
                <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value=""/>
                </span>
                <span id="spanOverSpeed" style="display:none">
               <input type="hidden" id="sdMinSpeed" name="sdMinSpeed" value="10" />
               <select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
							<script language="javascript">
                                document.write(maxVal);
                            </script>
                   </select>

                </span>
                <span id="spanOverStay" style="display:none">
                <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value=""/>&nbsp; in Mins
                </span>
                <span id="spanLowBattery" style="display:none">
                <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
                </span>
                <?php
			}
		}
		else
		{
			
		?>
        <span id="spanAlertDate" style="display:block">
        <input type="text" name="txtAlertDate" id="txtAlertDate" class="validate[required] text-input" readonly="true" tabindex="5" style="width:60%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
        </span>
        <span id="spanOdoMRead" style="display:none">
        <input type="text" name="txtOdoMRead" id="txtOdoMRead" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>
        </span>
        <span id="spanOverSpeed" style="display:none">
        	<input type="hidden" id="sdMinSpeed" name="sdMinSpeed" value="10" />
        	<select name="sdMaxSpeed" id="sdMaxSpeed" class="validate[required] text-input">
					<script language="javascript">
                        document.write(maxVal);
                    </script>
           </select>
        </span>
        <span id="spanOverStay" style="display:none">
        <input type="text" name="txtOverStay" id="txtOverStay" class="validate[required] text-input" tabindex="5" style="width:80%" value="<?php echo $deviceRecord[tdai_alertSrc];?>"/>&nbsp; in Mins
        </span>
        <span id="spanLowBattery" style="display:none">
        <input type="text" name="txtLowBattery" id="txtLowBattery" class="validate[required] text-input" readonly="readonly" tabindex="5" style="width:80%" value="10"/>&nbsp; in mV
        </span>
        
        <?php 
		}
		?>
        </td>
        
   </tr>  
   <tr id="trDoNot">
    <td style="vertical-align:middle">Do not Disturb Time</td>
     <td>      
    	<table>
          <tr>
            <td>From : <span><input type="text" name="time3" id="time3" style="width:60%" readonly="true" tabindex="5" value="<?php if($spltTime[0]) echo $spltTime[0]; else echo "10:00 AM";?>" /></span></td>
            <td>To : <span><input type="text" name="time4" id="time4" style="width:60%" readonly="true" tabindex="6" value="<?php if($spltTime[1]) echo $spltTime[1]; else echo "06:00 PM";?>" /></span></td>
          </tr>
        </table>      
    </td>
    </tr>
  <tr>
    <td align="right">Mobile / Email ID</td>
    <td align="left">
    <?php
		$getContInfo =  "SELECT * FROM tb_client_contact_info WHERE tcci_clientId  =".$_SESSION[clientID]." ORDER BY tcci_id ASC";
		$resContInfo = mysql_query($getContInfo);
	?>
         <select name="selContId" id="selContId" tabindex="7" style="width:80%" size="3" multiple="multiple" onChange="changeValue()" class="validate[required]" >
         <?php 
		
		
		while($fetContInfo = @mysql_fetch_assoc($resContInfo)) 
		{ 
			if(isset($_POST[txtAlertId]) && $deviceRecord['tdai_alertType'] == "" && $deviceRecord['tdai_source'] == "")
			{
				$geoArr = explode(",",$deviceRecord[tdai_contactIds]);
				if(in_array($fetContInfo[tcci_id],$geoArr))
				{
					$select = 'selected="selected"';
				}
				else $select ='';
			}
        ?>
        <option value="<?php echo $fetContInfo[tcci_id];?>" <?php echo $select ?>><?php echo $fetContInfo[tcci_source]; ?></option>
        <?php } ?>		
        </select>
        <input type="hidden" name="txtContIds" id="txtContIds" value="<?php echo $deviceRecord[tdai_contactIds];?>" />
    </td>
   </tr>
   <tr>
    <td align="right">Description</td>
    <td align="left">
	    <textarea name="txtAreaAlertDesc" maxlength="100" onKeyUp="return ismaxlength(this)" id="txtAreaAlertDesc" class="validate[required] text-input" cols="30" rows="2" tabindex="8"><?php echo $deviceRecord[tdai_description];?></textarea>    
    </td>
  </tr>
  <tr>
    <td style="text-align:center" colspan="2">    
    <?php if(isset($_POST[txtAlertId]) && $_POST[txtAlertId]!='')  { ?>
    <input type="submit" name="cmdUpdateAlert" id="cmdUpdateAlert" value="Update" class="save_btn" tabindex="9"  /> 
    <input type="button" name="map_cancel_btn" id="map_cancel_btn" value="Cancel" class="save_btn" onClick="location.href='index.php?ch=myalerts';" tabindex="10" /> 
	<?php } else { ?>
    <input type="submit" name="cmdAddAlert" id="cmdAddAlert" value="Add" class="save_btn" tabindex="8"/>
    <input type="button" name="map_cancel_btn" id="map_cancel_btn" value="Cancel" class="save_btn" onClick="location.href='index.php?ch=myalerts';" tabindex="9" /> 
    <?php } ?>
    </td>
  </tr>  
</table>
</form>
</td>
<td width="1%">&nbsp;</td>
<td style="vertical-align:top;" width="50%">
<?php
	$selInsur = "SELECT * FROM tb_device_alert_info,tb_deviceinfo WHERE tdai_active = 1 AND di_id = tdai_deviceId AND tdai_clientId = ".$_SESSION[clientID];
	
	if($_POST[txtAlertType] == "up" || !isset($_POST[txtAlertType]))
		$selInsur .= " AND tdai_status = 0";
	else if($_POST[txtAlertType] == "gone")
		$selInsur .= " AND tdai_status = 1";
	else if($_POST[txtAlertType] == "all")
		$selInsur = $selInsur;

	$resInsur = $db->query($selInsur);
?>
<table class="gridform_final" style="width:100%">
    <tr>
        <th>Device Name</th>
        <th>Purpose</th>
        <th>Alert Type.</th>
        <!--<th>Date/Reading</th>-->
        <th>Mobile / Email</th>
        <?php if($_POST[txtAlertType] == "gone") { ?>
        <th>Status</th>
        <th>Time</th>
        <?php } else { ?>       
        <th>Edit</th>
        <th>Delete</th>
        <?php } ?>
   </tr>
	 <?php 
	 if($db->affected_rows)
	 {
    while($fetInsur = @mysql_fetch_assoc($resInsur)) 
    { 
        if($fetInsur[di_deviceName])
            $devName = $fetInsur[di_deviceName];
        else
            $devName = $fetInsur[di_deviceId];
		
		$contactIds = "";	
		if($fetInsur[tdai_alertType] == "" && $fetInsur[tdai_source] == "")	
		{
			$contactIdArr = explode(",",$fetInsur[tdai_contactIds]);
			for($k =0; $k < count($contactIdArr)-1; $k++)
			{
				$sql2 = "SELECT * FROM tb_client_contact_info WHERE tcci_id = '".$contactIdArr[$k]."' AND tcci_clientId =".$_SESSION[clientID];
				$res2 = $db->query($sql2);
				while($fet2 = $db->fetch_array($res2))
				{
					$contactIds[]= $fet2[tcci_source].",";
				}
			}
		}
		else
		{
			$contactIds = explode(",",$fetInsur[tdai_source]);
		}		
    ?>
   <tr>
    <td><?php echo $devName;?></td>
    <td><?php echo $fetInsur[tdai_purpose];?></td>
    <td><?php if(strpos($fetInsur[tdai_alertBy],"_"))
				echo substr($fetInsur[tdai_alertBy],0,strpos($fetInsur[tdai_alertBy],"_")) ;
			  else 
			  	echo $fetInsur[tdai_alertBy];?>
    </td>
   <!-- <td><?php echo ucfirst($fetInsur[tdai_alertSrc]);?></td>-->
    <td><?php echo $contactIds[0].'...';?></td>
    <?php if($_POST[txtAlertType] == "gone" || $fetInsur[tdai_status] == 1) { ?>
    <td>Delivered</td>
    <td><?php echo date("h:i A",strtotime($fetInsur[tdai_deliveryTime]));?></td>
    <?php } else { ?>    
    <td><img src="../../images/edit.gif" style="cursor:pointer;" width="45" height="16" border="0" onClick="funEditUser('<?php echo $fetInsur[tdai_id];?>','<?php echo $_SESSION[clientID];?>','?ch=myalerts')" /></td>
    <td><img src="../../images/delete.gif" style="cursor:pointer;" width="45" height="16" border="0" onClick="changeSts('<?php echo $fetInsur[tdai_id];?>')" /></td>
    <?php } ?>
  </tr>
  <?php
	}
	 }
	 else
	 {
		 echo '<tr><td colspan="8">No Records Found</td></tr>';
	 }
  ?>
</table>
</td>
</tr>
</table>

<form name="frmSubmit" id="frmSubmit" method="post">
    <input type="hidden" name="txtAlertId" id="txtAlertId" />
    <input type="hidden" name="txtClientId" id="txtClientId" />
</form>

<form name="frmTypeSubmit" id="frmTypeSubmit" method="post">
    <input type="hidden" name="txtAlertType" id="txtAlertType" />
</form>